package tinybase.sm;

import tinybase.basic.RC;
import tinybase.basic.RedBase.AttrType;
import tinybase.basic.RedBase.CompOp;
import junit.framework.Assert;

public class ParserHelper {
	public static Node newnode(int type) {
		switch (type) {
		case Node.N_CREATETABLE:
			return new Node(Node.N_CREATETABLE, new CREATETABLE());
		case Node.N_CREATEINDEX:
			return new Node(Node.N_CREATEINDEX, new CREATEINDEX());
		case Node.N_DROPTABLE:
			return new Node(Node.N_DROPTABLE, new DROPTABLE());
		case Node.N_DROPINDEX:
			return new Node(Node.N_DROPINDEX, new DROPINDEX());
		case Node.N_LOAD:
			return new Node(Node.N_LOAD, new LOAD());
		case Node.N_SET:
			return new Node(Node.N_SET, new SET());
		case Node.N_HELP:
			return new Node(Node.N_HELP, new HELP());
		case Node.N_PRINT:
			return new Node(Node.N_PRINT, new PRINT());
		case Node.N_QUERY:
			return new Node(Node.N_QUERY, new QUERY());
		case Node.N_INSERT:
			return new Node(Node.N_INSERT, new INSERT());
		case Node.N_DELETE:
			return new Node(Node.N_DELETE, new DELETE());
		case Node.N_UPDATE:
			return new Node(Node.N_UPDATE, new UPDATE());
		case Node.N_RELATTR:
			return new Node(Node.N_RELATTR, new RELATTR());
		case Node.N_CONDITION:
			return new Node(Node.N_CONDITION, new CONDITION());
		case Node.N_RELATTR_OR_VALUE:
			return new Node(Node.N_RELATTR_OR_VALUE, new RELATTR_OR_VALUE());
		case Node.N_ATTRTYPE:
			return new Node(Node.N_ATTRTYPE, new ATTRTYPE());
		case Node.N_VALUE:
			return new Node(Node.N_VALUE, new VALUE());
		case Node.N_RELATION:
			return new Node(Node.N_RELATION, new RELATION());
		case Node.N_STATISTICS:
			Assert.assertTrue(false);
		case Node.N_LIST:
			return new Node(Node.N_LIST, new LIST());
		}
		Assert.assertTrue(false);
		return null;
	}
	
	public static Node createTableNode(String relname, Node attrlist) {
		Node n = ParserHelper.newnode(Node.N_CREATETABLE);
		CREATETABLE obj = (CREATETABLE)n.content;
		obj.relname = relname;
		obj.attrlist = attrlist;
		return n;
	}
	
	public static Node createIndexNode(String relname, String attrname) {
		Node n = ParserHelper.newnode(Node.N_CREATEINDEX);
		CREATEINDEX obj = (CREATEINDEX)n.content;
		obj.relname = relname;
		obj.attrname = attrname;
		return n;
	}
	
	/*
	 * drop_index_node: allocates, initializes, and returns a pointer to a new
	 * drop index node having the indicated values.
	 */
	public static Node dropIndexNode(String relname, String attrname) {
	    Node n = ParserHelper.newnode(Node.N_DROPINDEX);
	    DROPINDEX obj = (DROPINDEX)n.content;
	    obj.relname = relname;
	    obj.attrname = attrname;
	    return n;
	}
	
	/*
	 * drop_table_node: allocates, initializes, and returns a pointer to a new
	 * drop table node having the indicated values.
	 */
	public static Node dropTableNode(String relname) {
	    Node n = newnode(Node.N_DROPTABLE);
	    DROPTABLE obj = (DROPTABLE)n.content;
	    obj.relname = relname;
	    return n;
	}
	
	/*
	 * load_node: allocates, initializes, and returns a pointer to a new
	 * load node having the indicated values.
	 */
	public static Node loadNode(String relname, String filename) {
	    Node n = newnode(Node.N_LOAD);
	    LOAD obj = (LOAD)n.content;
	    obj.relname = relname;
	    obj.filename = filename;
	    return n;
	}
	
	/*
	 * set_node: allocates, initializes, and returns a pointer to a new
	 * set node having the indicated values.
	 */
	public static Node setNode(String paramName, String string) {
	    Node n = newnode(Node.N_SET);
	    SET obj = (SET)n.content;
	    obj.paramName = paramName;
	    obj.string = string;
	    return n;
	}
	
	/*
	 * help_node: allocates, initializes, and returns a pointer to a new
	 * help node having the indicated values.
	 */
	public static Node helpNode(String relname) {
	    Node n = newnode(Node.N_HELP);
	    HELP obj = (HELP)n.content;
	    obj.relname = relname;
	    return n;
	}
	
	/*
	 * print_node: allocates, initializes, and returns a pointer to a new
	 * print node having the indicated values.
	 */
	public static Node printNode(String relname) {
	    Node n = newnode(Node.N_PRINT);
	    PRINT obj = (PRINT)n.content;
	    obj.relname = relname;
	    return n;
	}
	
	/*
	 * query_node: allocates, initializes, and returns a pointer to a new
	 * query node having the indicated values.
	 */
	public static Node queryNode(Node relattrlist, Node rellist, Node conditionlist) {
	    Node n = newnode(Node.N_QUERY);
	    QUERY obj = (QUERY)n.content;
	    obj.relattrlist = relattrlist;
	    obj.rellist = rellist;
	    obj.conditionlist = conditionlist;
	    return n;
	}

	/*
	 * insert_node: allocates, initializes, and returns a pointer to a new
	 * insert node having the indicated values.
	 */
	public static Node insertNode(String relname,  Node valuelist) {
	    Node n = newnode(Node.N_INSERT);
	    INSERT obj = (INSERT)n.content;
	    obj.relname = relname;
	    obj.valuelist = valuelist;
	    return n;
	}
	
	/*
	 * delete_node: allocates, initializes, and returns a pointer to a new
	 * delete node having the indicated values.
	 */
	public static Node deleteNode(String relname,  Node conditionlist) {
	    Node n = newnode(Node.N_DELETE);
	    DELETE obj = (DELETE)n.content;
	    obj.relname = relname;
	    obj.conditionlist = conditionlist;
	    return n;
	}
	

	/*
	 * update_node: allocates, initializes, and returns a pointer to a new
	 * update node having the indicated values.
	 */
	public static Node updateNode(String relname, Node relattr, Node relorvalue, Node conditionlist) {
	    Node n = newnode(Node.N_UPDATE);
	    UPDATE obj = (UPDATE)n.content;
	    obj.relname = relname;
	    obj.relattr = relattr;
	    obj.relorvalue = relorvalue;
	    obj.conditionlist = conditionlist;
	    return n;
	}
	
	/*
	 * relattr_node: allocates, initializes, and returns a pointer to a new
	 * relattr node having the indicated values.
	 */
	public static Node relattrNode(String relname, String attrname) {
	    Node n = newnode(Node.N_RELATTR);
	    RELATTR obj = (RELATTR)n.content;
	    obj.relname = relname;
	    obj.attrname = attrname;
	    return n;
	}
	
	/*
	 * condition_node: allocates, initializes, and returns a pointer to a new
	 * condition node having the indicated values.
	 */
	public static Node conditionNode(Node lhsRelattr, CompOp op, Node rhsRelattrOrValue) {
	    Node n = newnode(Node.N_CONDITION);
	    CONDITION obj = (CONDITION)n.content;
	    obj.lhsRelattr = lhsRelattr;
	    obj.op = op;
	    obj.rhsRelattr = 
	      ((RELATTR_OR_VALUE)rhsRelattrOrValue.content).relattr;
	    obj.rhsValue = 
	      ((RELATTR_OR_VALUE)rhsRelattrOrValue.content).value;
	    return n;
	}
	
	/*
	 * value_node: allocates, initializes, and returns a pointer to a new
	 * value node having the indicated values.
	 */
	public static Node valueNode(AttrType type, Object value) {
	    Node n = newnode(Node.N_VALUE);
	    VALUE obj = (VALUE)n.content;
	    obj.type = type;
	    obj.val = value;
	    return n;
	}
	
	/*
	 * relattr_or_valuenode: allocates, initializes, and returns a pointer to 
	 * a new relattr_or_value node having the indicated values.
	 */
	public static Node relattrOrValueNode(Node relattr, Node value) {
	    Node n = newnode(Node.N_RELATTR_OR_VALUE);
	    RELATTR_OR_VALUE obj = (RELATTR_OR_VALUE)n.content;
	    obj.relattr = relattr;
	    obj.value = value;
	    return n;
	}
	
	/*
	 * attrtype_node: allocates, initializes, and returns a pointer to a new
	 * attrtype node having the indicated values.
	 */
	public static Node attrTypeNode(String attrname, String type) {
	    Node n = newnode(Node.N_ATTRTYPE);
	    ATTRTYPE obj = (ATTRTYPE)n.content;
	    obj.attrname = attrname;
	    obj.type = type;
	    return n;
	}
	
	/*
	 * relation_node: allocates, initializes, and returns a pointer to a new
	 * relation node having the indicated values.
	 */
	public static Node relationNode(String relname) {
	    Node n = newnode(Node.N_RELATION);
	    RELATION obj = (RELATION)n.content;
	    obj.relname = relname;
	    return n;
	}
	

	/*
	 * list_node: allocates, initializes, and returns a pointer to a new
	 * list node having the indicated values.
	 */
	public static Node listNode(Node n) {
	    Node list = newnode(Node.N_LIST);
	    LIST obj = (LIST)list.content;
	    obj.curr = n;
	    obj.next = null;
	    return list;		
	}
	
	/*
	 * prepends node n onto the front of list.
	 *
	 * Returns the resulting list.
	 */
	public static Node prepend(Node n, Node list) {
	    Node newlist = newnode(Node.N_LIST);
	    LIST obj = (LIST)newlist.content;
	    obj.curr = n;
	    obj.next = list;
	    return newlist;
	}

	void resetScanner() {
		
	}
	
	void resetCharptr() {
		
	}
	
	void new_query() {
		
	}
	
	RC   interp(Node n) {
		return null;
	}
	
	
}
